//
//  LBCardQuanVC.m
//  V-Learn
//
//  Created by Apple on 2019/1/18.
//  Copyright © 2019 yxk. All rights reserved.
//

#import "LBCardQuanVC.h"
#import "LBCardQuanCell.h"
#import "LBCardquanModel.h"
@interface LBCardQuanVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, assign) BOOL isUse;
@end

@implementation LBCardQuanVC

- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (UITableView *)tableView{
    if (_tableView == nil) {
        CGRect frame = CGRectMake(0, LBNAVBAR_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT-LBNAVBAR_HEIGHT);
        _tableView = [[UITableView alloc] initWithFrame:frame
                                                  style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupUI];
    self.isUse = YES;
}

- (void)setupUI{
    self.navigationItem.title = @"优惠券选择";
    self.tableView.backgroundColor = rgb(242,242,242);
    self.tableView.tableFooterView = [UIView new];
    
    [self.view addSubview:self.tableView];
    if (@available(iOS 11.0, *)) {
    self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;//UIScrollView也适用
    }else {
            self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    [self requestCard];
}
- (void)requestCard{
    //获取优惠券
    NSString *token = [UserModel getUserModel].token;
    NSMutableDictionary *data = [NSMutableDictionary dictionary];
    [data setObject:token forKey:@"token"];
//    [data setObject:@"1" forKey:@"state"];//1 未使用 2 已使用 3 已过期
    
    [data setObject:self.courseId forKey:@"courseId"];
    [data setObject:self.type forKey:@"type"];
    //新接口 WKQueryCourseCanUserVoucher_WKURL
    //旧接口 WKMycardQuan_WKURL 之前的接口不用了，在我的里面使用
    [WYHXNetWorkManager get:WKQueryCourseCanUserVoucher_WKURL params:data completionHandler:^(id responseObj, NSError *error) {
//        NSLog(@"%@",responseObj);
        NSString *code = responseObj[@"code"];
        if ([code integerValue]==200) {
            NSArray *arr = [LBCardquanModel mj_objectArrayWithKeyValuesArray:responseObj[@"data"]];
            [self.dataArray addObjectsFromArray:arr];
            
        }
        [self.tableView reloadData];
    }];
    
}
- (void)clickNoBtn:(UIButton *)sender{
    sender.selected = !sender.selected;
    self.isUse = NO;
    if (self.delegate && [self.delegate respondsToSelector:@selector(clickLBCardQuanVCCardQuandidselect::isUse:)]) {
        [self.delegate clickLBCardQuanVCCardQuandidselect:nil :self.type isUse:self.isUse];
    }
    [self.navigationController popViewControllerAnimated:YES];//直接返回
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LBCardQuanCell *cardCell = [LBCardQuanCell cellWithTableView:tableView];

    LBCardquanModel *model = self.dataArray[indexPath.section];
    cardCell.model = model;
    cardCell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cardCell;
}

#pragma mark - 点击
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    LBCardquanModel *model = self.dataArray[indexPath.section];
    if (self.delegate && [self.delegate respondsToSelector:@selector(clickLBCardQuanVCCardQuandidselect::isUse:)]) {
        [self.delegate clickLBCardQuanVCCardQuandidselect:model :self.type isUse:self.isUse];
        [self.navigationController popViewControllerAnimated:YES];
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPathP{
    return 150;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    if (section==0) {
        //    顶部灰色view
        UIView *sectionHeader = [UIView new];
        sectionHeader.backgroundColor = rgb(242,242,242);
        sectionHeader.frame = CGRectMake(0, 0, SCREEN_WIDTH, 52);
        
        
        //不使用优惠券按钮
        UIButton *noUseBtn = [[UIButton alloc] init];
        noUseBtn.layer.cornerRadius = 7.5;
        noUseBtn.layer.masksToBounds = YES;
        [sectionHeader addSubview:noUseBtn];
        [noUseBtn makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(sectionHeader);
            make.size.mas_equalTo(CGSizeMake(15, 15));
            make.left.mas_equalTo(15);
        }];
        [noUseBtn setImage:LBImage(@"wk_o") forState:UIControlStateNormal];
        [noUseBtn setImage:LBImage(@"wk_Selection") forState:UIControlStateSelected];
        [noUseBtn addTarget:self action:@selector(clickNoBtn:) forControlEvents:UIControlEventTouchUpInside];
        UILabel *lab = [UILabel labelWithText:@"不使用代金券" andTextColor:rgb(101,101,101) andName:nil andFontSize:16];
        [sectionHeader addSubview:lab];
        [lab makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(noUseBtn.mas_right).mas_equalTo(3);
            make.centerY.mas_equalTo(noUseBtn);
        }];
        
        return sectionHeader;
    }else{
        return nil;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section==0) {
            return 52;
    }else{
        return 0.001;
    }

}


@end
